﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using LeakView.common;
using LeakView.service;

namespace LeakView.misc.view
{
    public partial class FormMiscHistory2 : Form
    {

        private MiscType currentMiscType;
        private string currentMiscText;

        public FormMiscHistory2()
        {
            InitializeComponent();
            HideTabcontrolLabel(this.tabControl1);
        }

        public void HideTabcontrolLabel(TabControl tabControl1)
        {
            tabControl1.Appearance = TabAppearance.FlatButtons;
            tabControl1.SizeMode = TabSizeMode.Fixed;
            for (int i = 0; i < tabControl1.TabPages.Count; i++)
            {
                tabControl1.TabPages[i].Text = "";
            }
            tabControl1.ItemSize = new Size(0, 1);
        }

        private void FormMiscHistory2_Load(object sender, EventArgs e)
        {
            this.flowStatisticsGridView1.initialize();
            //this.miscGridView1.initialize();

            treeView1.Nodes.Clear();

            var head = AnyConfigs.get("MISC").get("HeadName").GetOrDefault("起点");
            var tail = AnyConfigs.get("MISC").get("TailName").GetOrDefault("终点");

            TreeNode node;
            
            node = treeView1.Nodes.Add("上游数据");
            node.Nodes.Add(buildTreeNode(head + "流量计总流量", MiscType.HeadLL));
            node.Nodes.Add(buildTreeNode(head + "瞬时流量", MiscType.HeadLS));
            node.Nodes.Add(buildTreeNode(head + "压力", MiscType.HeadYL));
            node.Nodes.Add(buildTreeNode(head + "温度", MiscType.HeadWD));
            node.Nodes.Add(buildTreeNode(head + "流量积算", MiscType.Head15M));

            node = treeView1.Nodes.Add("下游数据");
            node.Nodes.Add(buildTreeNode(tail + "流量计总流量", MiscType.TailLL));
            node.Nodes.Add(buildTreeNode(tail + "瞬时流量", MiscType.TailLS));
            node.Nodes.Add(buildTreeNode(tail + "压力", MiscType.TailYL));
            node.Nodes.Add(buildTreeNode(tail + "温度", MiscType.TailWD));
            node.Nodes.Add(buildTreeNode(tail + "流量积算", MiscType.Tail15M));

            node = treeView1.Nodes.Add("输差数据");
            node.Nodes.Add(buildTreeNode("瞬时输差", MiscType.DeltaNOW));
            node.Nodes.Add(buildTreeNode("10分钟输差", MiscType.Delta10M));
            node.Nodes.Add(buildTreeNode("30分钟输差", MiscType.Delta30M));
            node.Nodes.Add(buildTreeNode("60分钟输差", MiscType.Delta60M));

            treeView1.ExpandAll();
        }

        private TreeNode buildTreeNode(string text, MiscType type)
        {
            TreeNode node = new TreeNode
            {
                Text = text,
                Tag = type
            };
            return node;
        }

        private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point ClickPoint = new Point(e.X, e.Y);
            TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
            if (CurrentNode is TreeNode)
            {
                if(CurrentNode.Tag is MiscType)
                {

                    this.currentMiscType = (MiscType)CurrentNode.Tag;
                    this.currentMiscText = CurrentNode.Text;

                    this.listView1.Clear();

                    ThreadPool.QueueUserWorkItem(callback =>
                    {
                        List<string> files = MiscModal.me.LoadFileList(currentMiscType);

                        treeView1.Invoke(new Action(() =>
                        {
                            files?.ForEach(f =>
                            {
                                this.listView1.Items.Add(f);
                            });
                        }));
                    });

                }
            }
        }

        private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {

            

            int viewIndex = 0;  // 0:曲线， 1:积算， 2:表格

            switch(this.currentMiscType)
            {
                case MiscType.Head15M:
                case MiscType.Tail15M:
                    viewIndex = 1;
                    break;
                case MiscType.Delta10M:
                case MiscType.Delta30M:
                case MiscType.Delta60M:
                    viewIndex = 2;
                    break;
                default:
                    viewIndex = 0;
                    break;
            }

            this.tabControl1.SelectedIndex = viewIndex;

            //bool tableView = false;
            //bool tableView = (this.currentMiscType == MiscType.Head15M || this.currentMiscType == MiscType.Tail15M);
            //this.splitContainer1.Panel1Collapsed = tableView;
            //this.splitContainer1.Panel2Collapsed = !tableView;

            if (this.listView1.SelectedItems.Count == 0) return;

            string filename = (string)this.listView1.SelectedItems[0].Text;

            this.chartMiscHistory1.clear();
            this.flowStatisticsGridView1.clearValues();
            this.miscValueGrid1.clear();

            ThreadPool.QueueUserWorkItem(callback =>
            {
                var data = MiscModal.me.LoadHistory(currentMiscType, filename);

                listView1.Invoke(new Action(() =>
                {
                    if (viewIndex == 1)
                    {
                        this.flowStatisticsGridView1.setValues(data);
                    }
                    else if(viewIndex == 0)
                    {
                        data.ForEach(d =>
                        {
                            this.chartMiscHistory1.appendValue(
                                Tools.ConvertLongToDateTime(d.timestamp),
                                MiscModal.me.GetDoubleFromMiscValue(d));

                        });

                        this.chartMiscHistory1.Title =
                            this.currentMiscText + "(" + MiscTypeUtil.ToUnit(this.currentMiscType) + ")";

                    }
                    else if (viewIndex == 2)
                    {
                        this.miscValueGrid1.addValues(data);
                    }

                }));
            });

        }
    }
}
